﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using Business;
using Data;

public partial class client_Client : System.Web.UI.Page
{
	private String clientname, clientid;
	private bool reporttype = false;
	private string[] siteselected;
	private string[] componentIDs2;
	private string[] componentIDs;
	private string surveydate;
	private bool oldreport_cb;
	private int i;

	protected void Page_Load(object sender, EventArgs e)
	{
		i = 0;
		componentIDs2 = new string[100];

		reporttype = (bool) Session["OilReport"];
		clientname = (String) Session["clientName"];
		clientid = (String) Session["clientid"];
		siteselected = (String[]) Session["sitename"];
		surveydate = (string) Session["SurveyDate"];
		int size = siteselected.Length;
		if (!IsPostBack)
		{
			if (reporttype == false)
			{
				oldreport_cb = (bool) Session["OldReport"];
				Generate_VibrationTreeView();
			}
			else
			{
				Generate_OilTreeView();
			}
		}

		componentIDs = new string[i];
		for (int j = 0; j < i; j++)
		{
			componentIDs[j] = componentIDs2[j];
		}
		Session["componentIDS"] = componentIDs;
		if (!reporttype)
		{
			report_content.Attributes["src"] = "Welcome.aspx";
		}
		else
		{
			report_content.Attributes["src"] = "OilReport_Mangage.aspx";
		}
	}

	protected void Generate_VibrationTreeView()
	{
		CreateConn objcon;

		try
		{
			var rootnode = new TreeNode(clientname);
			rootnode.SelectAction = TreeNodeSelectAction.SelectExpand;
			rootnode.NavigateUrl = "Welcome.aspx";
			rootnode.Target = "report_content";

			tree1.Nodes.Add(rootnode);
			foreach (string sitename in siteselected)
			{
				var secnode = new TreeNode(sitename);
				rootnode.ChildNodes.Add(secnode);
				objcon = new CreateConn();
				string query =
					"SELECT m.machine_id, m.machineName FROM Machines AS m INNER JOIN Sites AS s ON m.site_id = s.site_id WHERE s.siteName ='" +
					sitename + "'";
				secnode.SelectAction = TreeNodeSelectAction.SelectExpand;
				if (objcon == null)
				{
					objcon = new CreateConn();
				}
				DataTable dt = objcon.SQLConnDataTable(query);

				if (dt.Rows.Count > 0)
				{
					foreach (DataRow r in dt.Rows)
					{
						TreeNode thirdnode = new TreeNode(r["machineName"].ToString());
						string machineid = r["machine_id"].ToString();
						string machinename = r["machineName"].ToString();
						thirdnode.ImageUrl = GetIcon(machineid);
						secnode.ChildNodes.Add(thirdnode);
						string query2 = "SELECT component_id,componentName from Components where machine_id ='" +
						                machineid + "'";
						objcon = new CreateConn();
						DataTable component_dt = objcon.SQLConnDataTable(query2);
						bool lastcomponent = false;
						foreach (DataRow r2 in component_dt.Rows)
						{
							string compname = r2["componentName"].ToString();
							string componentid = r2["component_id"].ToString();
							componentIDs2[i] = componentid;
							i++;

							TreeNode fourthnode = new TreeNode(r2["componentName"].ToString());
							thirdnode.ChildNodes.Add(fourthnode);
							if (r2 == component_dt.Rows[component_dt.Rows.Count - 1])
							{
								lastcomponent = true;
							}
							if (oldreport_cb)
							{
								Comment cmt = new Comment();
								cmt.copyOldReport(Int32.Parse(componentid), surveydate);
							}

							fourthnode.NavigateUrl = "Newreport.aspx?machinename=" + machinename + "&componentname=" +
							                         compname +
							                         "&componentID=" + componentid + "&lastcomponent=" +
							                         lastcomponent + "&machineid=" + machineid;
							fourthnode.Target = "report_content";
						}
					}
				}
			}
		}
		catch (Exception e)
		{
		}
	}

	protected void Generate_OilTreeView()
	{
		var clientNode = new TreeNode(clientname);
		clientNode.NavigateUrl = "Welcome.aspx";
		clientNode.Target = "report_content";
		tree1.Nodes.Add(clientNode);
		tree1.ExpandDepth = 3;
		if (siteselected.Count() == 0)
		{
			return;
		}
		var ltSiteNames = new List<string>();
		foreach (string siteName in siteselected)
		{
			ltSiteNames.Add(string.Format("'{0}'", siteName));
		}

		var sites = String.Join(",", ltSiteNames.ToArray());
		sites = string.Format("({0})", sites);
		string getSitesQry = "select DISTINCT(site_id),siteName FROM [oilComponents] WHERE siteName in " + sites +
		                     " and client_id = '" + clientid +
		                     "' ORDER BY site_id";
		var objcon_GetSites = new CreateConn();
		var dtSites = objcon_GetSites.SQLConnDataTable(getSitesQry);
		bool lastcomponent = false;
		if (dtSites.Rows.Count > 0)
		{
			foreach (DataRow dr_site in dtSites.Rows)
			{
				String sitename = dr_site["siteName"].ToString();

				var Node_site = new TreeNode(sitename);
				clientNode.ChildNodes.Add(Node_site);
				Node_site.CollapseAll();
				string query_machines =
					"SELECT DISTINCT  machine_id,machineName FROM [oilComponents] WHERE(site_id ='" +
					dr_site["site_id"] + "')ORDER BY machine_id";
				var machinobjcon = new CreateConn();
				int machine_status = 0;
				DataTable dt_machines = machinobjcon.SQLConnDataTable(query_machines);

				foreach (DataRow dr_machine in dt_machines.Rows)
				{
					string machineid = dr_machine["machine_id"].ToString();
					string machinename = dr_machine["machineName"].ToString();

					var Node_machine = new TreeNode(machinename);
					Node_site.ChildNodes.Add(Node_machine);
					Node_machine.Collapse();
					String query_components =
						"SELECT DISTINCT component_id, componentName, * FROM [oilComponents] WHERE (machine_id='" +
						dr_machine["machine_id"] + "')  ";
					CreateConn objconncomponents = new CreateConn();
					DataTable dt_components = objconncomponents.SQLConnDataTable(query_components);
					if (dt_components.Rows.Count > 0)
					{
						foreach (DataRow dr_component in dt_components.Rows)
						{
							int component_id = Int32.Parse(dr_component["Component_id"].ToString());

							string query_status = "select status from OilReports where Component_id='" +
							                      component_id + "'";
							CreateConn obj_querystatus = new CreateConn();
							try
							{
								DataTable dt_querystatus = obj_querystatus.SQLConnDataTable(query_status);
								if (dt_querystatus != null && dt_querystatus.Rows.Count > 0)
								{
									foreach (DataRow dr_status in dt_querystatus.Rows)
									{
										string component_status = dr_status["status"].ToString();
										int com_sta_int = HelperClass.StatusToInt(component_status);
										if (com_sta_int > machine_status)
										{
											machine_status = com_sta_int;
										}
									}
								}
							}
							catch (Exception e_status)
							{
								Response.Write(e_status.ToString());
							}

							string compname = dr_component["componentName"].ToString();
							string componentid = dr_component["component_id"].ToString();
							componentIDs2[i] = componentid;
							i++;
							var nodeComponent = new TreeNode(compname);
							if (dr_component == dt_components.Rows[dt_components.Rows.Count - 1])
							{
								lastcomponent = true;
							}
							nodeComponent.NavigateUrl = "OilReport_Mangage.aspx?machinename=" + dr_component["machineName"] +
							                            "&componentname=" + compname +
							                            "&componentID=" + componentid + "&lastcomponent=" +
							                            lastcomponent + "&machineid=" + machineid;
							nodeComponent.Target = "report_content";
							Node_machine.ChildNodes.Add(nodeComponent);
						}
						string status_string = machine_status.ToString();
						Node_machine.ImageUrl = HelperClass.GetOilIconByStatusID(status_string);
					}
				}
			}
		}
	}

	protected string GetIcon(string machineID)
	{
		string src = String.Empty;
		string query = "SELECT   MAX(status_id) AS status_id"
		               + " FROM      ComponentStatus"
		               + " WHERE   (component_id IN"
		               + " (SELECT   component_id"
		               + " FROM      Components"
		               + " WHERE   (machine_id = '" + machineID + "'))) AND (dateRecorded IN"
		               + " (SELECT   MAX(dateRecorded) AS dateRecorded"
		               + " FROM      ComponentStatus AS ComponentStatus_1"
		               + " WHERE   (component_id in (select component_id From Components WHERE machine_id='" + machineID +
		               "'))))";
		var objcon = new CreateConn();
		DataTable dt = objcon.SQLConnDataTable(query);
		foreach (DataRow dr in dt.Rows)
		{
			string statusID = dr["status_id"].ToString();
			src = HelperClass.GetVibartionIconByStatusID(statusID);
		}
		return src;
	}

	protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
	{
		Session.Abandon();
		Session.Contents.RemoveAll();
		FormsAuthentication.SignOut();
	}
}
